package br.ita.redecasd.mi.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.icu.util.StringTokenizer;

import br.ita.redecasd.mi.ws.SessionHandlerWSProxy;

/**
 * Servlet implementation class for Servlet: AclServlet
 *
 */
 public class AclServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public AclServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if ( request.getSession() != null && request.getSession().getAttribute("username") != null)
		{
			String username = (String)request.getSession().getAttribute("username");
			String sessionID = (String)request.getSession().getAttribute("sessionID");
			String userAgent = request.getHeader("User-Agent"); 
			String patientName = (String)request.getSession().getAttribute("patientName");
			SessionHandlerWSProxy proxy = new SessionHandlerWSProxy();
	        boolean sessionValid = proxy.validateSession(sessionID, username);
	        if ( sessionValid )
	        {
	        	byte[] signature = restoreSignature(request.getParameter("signature") );
	        	String validated = proxy.validateSign(patientName, userAgent, signature);
	        	if ( validated  != null )
	        	{
	        		proxy.updateACL(username, patientName);
	        		String[] patientInfo = proxy.getPatientDetails(patientName);
		        	if ( patientInfo != null )
		        	{
		        		request.setAttribute("login", patientInfo[0]);
		        		request.setAttribute("displayName", patientInfo[1]);
		        		request.setAttribute("phoneNumber", patientInfo[2]);
		        		request.setAttribute("address", patientInfo[3]);
		        		RequestDispatcher view = request.getRequestDispatcher("info.jsp");
		        		view.forward(request,response);
		        		return;
		        	}
	        	}
	        }
	        
		}
		
		RequestDispatcher view = request.getRequestDispatcher("welcome.jsp");
		view.forward(request, response);
		
		
	}
	
	private byte[] restoreSignature( String sigInStr )
	{
		StringTokenizer st = new StringTokenizer(sigInStr, ";");
		byte[] sign_in_bytes = new byte[st.countTokens()];
		for ( int i = 0; i < sign_in_bytes.length; i++ )
			sign_in_bytes[i] = Byte.parseByte(st.nextToken());
		
		return sign_in_bytes;
	}
	   	  	    
}